﻿using System;
using Sustainalytics.Reporting.Framework;
using Foxit.PDF.PageElements;
using Foxit.PDF;
using Foxit.PDF.Merger;
using Sustainalytics.Entities;

namespace Sustainalytics.RiskRatings.Reporting
{
    public class FinalSection: PdfReportSectionGenerator
    {
        public FinalSection(int index):base(index)
        {
            this._reportSection.Name = "Final section";
        }

        public override void ComposeSectionContent()
        {
            var pdfDocument = new PdfDocument(_dataSource.Glossary);
            isCoreReport = _dataSource.ReportType == ReportType.CoreRiskRatingsReport || _dataSource.ReportType == ReportType.SummaryCoreRiskRatingsReport;
            for (var number = 1; number <= pdfDocument.Pages.Count; number++)
            {
                PdfPage importedPage = pdfDocument.GetPage(number);
                ImportedPageArea importedPageArea = new ImportedPageArea(importedPage, -_report.PageMargins, -_report.PageMargins - 30, 1f);
                PdfReportPage page;
                if (isCoreReport)
                    page = new PdfReportPage_Core(_report, _dataSource, 4);
                else
                    page = new PdfReportPage_RiskRatingOverview(_report, _dataSource, true);
                _report.Pages.Add(page);
                page.AddElement(importedPageArea);
                if (number == 1)
                    OutlinePositions.Add(new OutlinePosition { Name = "Glossary of terms", PageNumber = page.Report.Pages.Count, VerticalPosition = page.Top - HeaderHeight });
            }
            AddDisclaimer();
        }

        private void AddDisclaimer()
        {
            var width = _report.PageMaxWidth;
            var height = _report.PageMaxHeight - 25f;
            PdfReportPage page;
            if (isCoreReport)
            {
                page = new PdfReportPage_Core(_report, _dataSource, 4);
                OutlinePositions.Add(new OutlinePosition { Name = "Disclaimer", PageNumber = OutlinePageCount, VerticalPosition = page.Top - HeaderHeight });
            }
            else
                page = new PdfReportPage_RiskRatingOverview(_report, _dataSource, true);
            _report.Pages.Add(page);
            float top = page.Top + (float)VerticalSpacing.Small;

            
            const float fontSize = 7;
            var style = new FormattedTextAreaStyle(Fonts.RobotoFontFamily, fontSize, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };

            var copyright1 = $"Copyright © {DateTime.UtcNow.Year} Sustainalytics. All rights reserved.";


            var firstColumnWidth = width * 0.35f;
            var seconfColumnWidth = (width * 0.65f) - 10f;

            var table = GetDefaultTable(_report.LeftMargin, top, width, height);


            table.Columns.Add(firstColumnWidth);
            table.Columns.Add(seconfColumnWidth);

            const string title = "Disclaimer";
            var row = table.Rows.Add(fontSize);
            row.Cells.Add(new Label(title.ToUpper(), 0.0f, 0.0f, firstColumnWidth,
                    (float)HeaderTextSizes.HeaderReportTitle, Fonts.MontserratRegular, (float)HeaderTextSizes.HeaderReportTitle));

            row = table.Rows.Add(fontSize);
            row.Cells.Add(string.Empty);
            row.Cells.Add(string.Empty);

            row = table.Rows.Add(fontSize);
            row.Cells.Add(string.Empty);
            row.Cells.Add(new Label(copyright1, 0.0f, 0.0f, seconfColumnWidth,
                    fontSize + 2f, Fonts.RobotoBold, fontSize));


            var disclaimer = "<p>The ownership and all intellectual property rights to this publication/report and the information contained herein are vested exclusively in Sustainalytics and/or its suppliers. Unless otherwise expressly agreed in writing between you and Sustainalytics, you will not be permitted to use this information otherwise than for internal use, nor will you be permitted to reproduce, disseminate, comingle, create derivative works, furnish in any manner, make available to third parties or publish this publication/report, parts hereof or the information contained herein in any form or in any manner, be it electronically, mechanically, through photocopies, recordings.  The information on which this publication/report is based on reflects the situation as on the date of its elaboration. Such information has – fully or partially – been derived from third parties and is therefore subject to continuous modification. " +
                "THE INFORMATION HEREIN IS PROVIDED SOLELY FOR INFORMATIONAL PURPOSES AND THEREFORE ARE NOT AN OFFER TO BUY OR SELL A SECURITY. NEITHER SUSTAINALYTICS NOR ALL ITS THIRD-PARTY SUPPLIERS PROVIDE INVESTMENT ADVICE (AS DEFINED IN THE APPLICABLE JURISDICTION) OR ANY OTHER FORM OF (FINANCIAL) ADVICE AND NOTHING WITHIN THIS PUBLICATION/REPORT CONSTITUTES SUCH ADVICE. SUSTAINALYTICS OBSERVES THE GREATEST POSSIBLE CARE IN USING INFORMATION, HOWEVER THE INFORMATION IS PROVIDED “AS IS” AND NEITHER SUSTAINALYTICS NOR ITS SUPPLIERS ACCEPT ANY LIABILITY FOR DAMAGE ARISING FROM THE USE OF THIS PUBLICATION/REPORT OR INFORMATION CONTAINED HEREIN IN ANY MANNER WHATSOEVER. MOREOVER, SUSTAINALYTICS AND ALL ITS THIRD-PARTY SUPPLIERS DISCLAIM ANY AND ALL WARRANTIES AND REPRESENTATIONS, EXPRESS OR IMPLIED, INCLUDING ANY WARRANTIES OF MERCHANTIBILITY, COMPLETENESS, ACCURACY OR FITNESS FOR A PARTICULAR PURPOSE.</p>";


            var formattedTextArea = new FormattedTextArea(disclaimer, 0.0f, 0.0f, seconfColumnWidth, 500f, style);
            var textAreaHeight = formattedTextArea.GetRequiredHeight() + 5f;
            formattedTextArea = new FormattedTextArea(disclaimer, 0.0f, 0.0f, seconfColumnWidth, textAreaHeight, style);

            row = table.Rows.Add(textAreaHeight);
            row.Cells.Add(string.Empty);
            row.Cells.Add(formattedTextArea);


            row = table.Rows.Add(fontSize);
            row.Cells.Add(string.Empty);
            row.Cells.Add(string.Empty);



            disclaimer = "<p>This publication/report may contain proprietary information from third parties (Third Party Data) and here you can find additional terms and conditions imposed by the following Third Party Data providers regarding the use of their data:</ p>";

            formattedTextArea = new FormattedTextArea(disclaimer, 0.0f, 0.0f, seconfColumnWidth, 500f, style);
            textAreaHeight = formattedTextArea.GetRequiredHeight() + 5f;
            formattedTextArea = new FormattedTextArea(disclaimer, 0.0f, 0.0f, seconfColumnWidth, textAreaHeight, style);

            row = table.Rows.Add(textAreaHeight);
            row.Cells.Add(string.Empty);
            row.Cells.Add(formattedTextArea);


            row = table.Rows.Add(fontSize);
            row.Cells.Add(string.Empty);
            row.Cells.Add(string.Empty);



            const string text = "www.sustainalytics.com/legal-disclaimers";
            var font = Fonts.RobotoBold;

            // Create the link's label
            var label = new Label(text, 0.0f, 0.0f, seconfColumnWidth, 10f, font, fontSize, new RgbColor(240, 171, 0));

            // Set the action then create the link
            var action = new UrlAction("www.sustainalytics.com/legal-disclaimers");
            var link = new Link(0, 0, font.GetTextWidth(text, fontSize), 20, action);

            // Add the label and the link to the page
            var elements = new Group();
            elements.Add(label);
            elements.Add(link);


            row = table.Rows.Add(fontSize);
            row.Cells.Add(string.Empty);
            row.Cells.Add(elements);




            page.Elements.Add(table);
        }
    }
}
